﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;

namespace IRC.IRepairClouds.DataLayer
{
    public static class JSONBaseUtils
    {
        public static JavaScriptSerializer Serializer;

        static JSONBaseUtils()
        {
            Serializer = new JavaScriptSerializer(new SimpleTypeResolver());
        }

        public static string JSON_BaseSerialize(this object obj)
        {
            return Serializer.Serialize(obj);
        }

        public static T JSON_BaseDeserialize<T>(this string json)
        {
            if (json == null)
                return default(T);
            return Serializer.Deserialize<T>(json);
        }

        public static string JSON_BaseSerializeEx(this object obj)
        {
            try
            {
                return Serializer.Serialize(obj);
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static T JSON_BaseDeserializeEx<T>(this string json)
        {
            try
            {
                if (json == null)
                    return default(T);
                return Serializer.Deserialize<T>(json);
            }
            catch (Exception)
            {
                return default(T);
            }
        }
    }
}
